package com.zhihuishu.micro.course.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zhihuishu.micro.course.dao.CcCourseMapper;
import com.zhihuishu.micro.course.dao.LessonMapper;
import com.zhihuishu.micro.course.dao.TblCourseMapper;
import com.zhihuishu.micro.course.dto.CourseAuditDto;
import com.zhihuishu.micro.course.model.CcCourse;
import com.zhihuishu.micro.course.model.Lesson;
import com.zhihuishu.micro.course.model.TblCourse;
import com.zhihuishu.micro.course.service.CourseAuditService;
import com.zhihuishu.toolkit.helper.BeanHelper;

@Service("courseAuditService")
public class CourseAuditServiceImpl extends BaseService implements CourseAuditService {
	
	@Resource
	private TblCourseMapper tblCourseMapper ;
	@Resource
	private CcCourseMapper ccCourseMapper ;
	@Resource
	private LessonMapper lessonMapper ;
	
	@Override
	public Integer findPendingAuditCourseAmount(String courseName) {
		return tblCourseMapper.findPendingAuditCourseAmount(courseName);
	}
	
	@Override
	public List<TblCourse> findPendingAuditCourseDtos(String courseName,
			int pageNo, int pageSize) {
		Integer startPos = (pageNo-1)*pageSize;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("courseName", courseName);
		map.put("startPos", startPos);
		map.put("pageSize", pageSize);
		return tblCourseMapper.findPendingAuditCourseDtos(map);
	}

	@Override
	public List<CourseAuditDto> findCourseOpenDtos(int count) {
		List<TblCourse> list=tblCourseMapper.findCourseOpenDtos(count);
		List<CourseAuditDto> auditList = null;//查询内容
		if(!CollectionUtils.isEmpty(list)){
			auditList = new ArrayList<CourseAuditDto>();
			CourseAuditDto courseAuditDto = null;
			for(TblCourse tblCourse:list){
				courseAuditDto = BeanHelper.transTo(tblCourse, CourseAuditDto.class);
				courseAuditDto.setUserId(tblCourse.getMajorSpeaker());
				if(tblCourse.getCcCourse()!=null){
					courseAuditDto.setCourseTag(tblCourse.getCcCourse().getCourseTag());
					courseAuditDto.setTeamData(tblCourse.getCcCourse().getTeamData());
					courseAuditDto.setTeachingProgram(tblCourse.getCcCourse().getTeachingProgram());
					courseAuditDto.setTeachingPlan(tblCourse.getCcCourse().getTeachingPlan());
					courseAuditDto.setMoiveDesc(tblCourse.getCcCourse().getMoiveDesc());
				}
				auditList.add(courseAuditDto);
				if(tblCourse.getCourseId()!=null){
					tblCourseMapper.updateQueryState(tblCourse.getCourseId());
				}
				
			}
			
		}
		return auditList;
	}

	@Override
	public Boolean updateCourseOpenDtos(List<CourseAuditDto> courseOpenDtoList) {
		TblCourse tblCourse = null;
		CcCourse ccCourse = null;
		Boolean flag = true;
		try {
			for(CourseAuditDto courseAuditDto:courseOpenDtoList){
				if(courseAuditDto.getCourseId()!=null){
					tblCourse = BeanHelper.transTo(courseAuditDto, TblCourse.class);
					tblCourseMapper.update(tblCourse) ;
					Long linkId = tblCourseMapper.getLinkId(courseAuditDto.getCourseId());
					if(linkId!=null){
						ccCourse = BeanHelper.transTo(courseAuditDto, CcCourse.class);
						ccCourse.setId(linkId);
						ccCourseMapper.update(ccCourse);
					}
					
				}
			}
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public List<Lesson> findLessonlistByChapterId(Integer chapterId) {
		return lessonMapper.findLessonlistByChapterId(chapterId);
	}
	
}
